package com.jg.utils.reflex.classes;

import com.jg.utils.reflex.en.Animal;
import com.jg.utils.reflex.en.MyInterface;
import com.jg.utils.reflex.en.User;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ClassTest {

    /**
     * 获取类
     *
     * @throws ClassNotFoundException
     */
    @Test
    public void testClass1() throws ClassNotFoundException {
// 通过类名.class获取class
        Class<User> userClass = User.class;
        // class.getName() 获取类完成包名 com.jg.pojo.User
        System.out.println(userClass.getName());
//获得类的名字
        System.out.println(userClass.getSimpleName());
        // 获取class 的第二中方法
        Class<?> userClss2 = Class.forName("com.jg.utils.reflex.en.User");
        System.out.println("第二中" + userClss2.getSimpleName());

        //获取class 的第三中方法
        User user = new User();

        Class<? extends User> userClass3 = user.getClass();

        System.out.println("第三中" + userClass.getSimpleName());

    }

    @Test
    public void testClaass2() throws InstantiationException, IllegalAccessException {
        Class<User> userClass = User.class;
//获取包
        System.out.println(userClass.getPackage());
        //获取user对象
        User user = userClass.newInstance();
        //获取类中的类
        Class<?>[] classes = userClass.getClasses();
        System.out.println("获取类中的公共类：");
        for (Class<?> aClass : classes
        ) {
            System.out.println(aClass.getSimpleName());

        }
        //获取所以类中的类
        System.out.println("获取类中的所以类：");
        Class<?>[] classes1 = userClass.getDeclaredClasses();
        for (Class<?> aClass : classes1
        ) {
            System.out.println(aClass.getSimpleName());

        }

    }
    @Test
    public void testExtends(){
        Class<User> userClass = User.class;
        Class<? extends Animal> aClass = userClass.asSubclass(Animal.class);
        System.out.println(aClass.getName());

        System.out.println("获取父类");

        Class<? super User> superClass = userClass.getSuperclass();
        System.out.println(superClass.getName());

        System.out.println("获取接口");

        Class<?>[] interfaces = userClass.getInterfaces();
        for (Class<?> anInterface : interfaces
             ) {
            System.out.println(anInterface);

        }
    }

    @Test
    public void  testClassField(){
        Class<User> userClass = User.class;
        Field[] fields = userClass.getFields();
        for (Field field:fields
             ) {
            System.out.println("获得所有公有的字段："+field.getName());
        }
        System.out.println("获得所有字段对象");

        Field[] declaredFields = userClass.getDeclaredFields();
        for (Field de: declaredFields) {
            System.out.println(de.getName());
        }
    }

 @Test
    public  void testNew() throws Exception{
        //新建实体类
     Class<User> userClass = User.class;
     User user = userClass.newInstance();
     System.out.println( user.toString() );
 }
 @Test
    public void testAnnotation(){
     Class<User> userClass = User.class;
     Logger logger = userClass.getAnnotation(Logger.class);
     System.out.println(logger);
 }

 @Test
    public void testClassConsructor() throws Exception{
     Class<User> userClass = User.class;
     //获取该类中的所有的公共构造方法
     Constructor<?>[] constructors = userClass.getConstructors();
     for (Constructor<?> constructor: constructors) {
         int parameterCount = constructor.getParameterCount();
         System.out.println(parameterCount+"个参数的构造方法"+constructor);
     }


     //获取该类中的所有的构造方法
     Constructor<?>[] constructors1 = userClass.getDeclaredConstructors();
     for (Constructor<?> constructor: constructors1) {
         int parameterCount = constructor.getParameterCount();
         System.out.println("所有构造方法");
         System.out.println(parameterCount+"个参数的构造方法"+constructor);
     }
//获取该类中的所有的构造方法 ,传入类型
     Constructor<?> declaredConstructor = userClass.getDeclaredConstructor(String.class);

     System.out.println(declaredConstructor);

 }

    /**
     * 获取类中的方法
     */
 @Test
    public void testMethod() throws Exception{
     Class<User> userClass = User.class;
     Method method = userClass.getDeclaredMethod("setAge", Integer.class);

     System.out.println(method.getName());
 }

 @Test
    public void testClassOtherMethod() throws  Exception{
     Class<User> userClass = User.class;

     System.out.println(Logger.class.isAnnotation());
     //如果是注解类型则返回true
     System.out.println(userClass.isAnnotation());
     System.out.println(userClass.isInterface());
     //如果是接口类则返回true
     System.out.println(MyInterface.class.isInterface());

     User user = new User();
     //如果obj是该类的实例则返回true
     System.out.println(userClass.isInstance(user));

//如果是指定类型注解类型则返回true
     System.out.println(userClass.isAnnotationPresent(Logger.class));
 }





}
